package br.com.android.aluno.util;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import br.com.android.aluno.model.Aluno;

public class UtilJson {
	
	public static String getJsonArray(List<Aluno> alunos) throws JSONException, ParseException {
		JSONArray jSonArray = new JSONArray();
		
		for (int i = 0; i < alunos.size(); i++) {
			JSONObject jObject = alunos.get(i).toJson();
			jSonArray.put(jObject);
		}

		return jSonArray.toString();
	}
	
	public static List<Aluno> getJsonArray(String jsonArrayStr) throws JSONException, ParseException {
		List<Aluno> alunos = null;
		JSONArray jsonArray = new JSONArray(jsonArrayStr);
		
		if(jsonArray.length() > 0){
			alunos = new ArrayList<Aluno>();
			
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject obj = (JSONObject) jsonArray.get(i);
								
				Aluno aluno = new Aluno(obj);
				alunos.add(aluno);
			}
		}
		
		return alunos;
	}
}
